from django import template
from django.utils.html import format_html
from myadmin import models
register = template.Library()

# 商品分类 缩进层缩进
@register.simple_tag
def sj(paths):
	cou = paths.count(",")
	st = ""
	if cou>1:
		for i in range(cou):
			st+="|---"
	return st
# 商品分类   ---分页
@register.simple_tag
def showCatesPage(count,request):
	try:
		p = int(request.GET.get('p',1))
	except:
		p=1
	begin = p-4
	end = p+5
	count = int(count) 
	if p<5:
		begin = 1
		end = 10
	if count<10:
		begin = 1
		end = count
	if p>count-5:
		begin = count-9
		end = count
	if begin<1:
		begin = 1
	# {% for i in data.paginator.page_range %}
 #                <li>
 #                  <a href="?p={{i}}">{{i}}</a></li>
 #                  {% endfor %}
                  # <li>

                  #   

                  # <li><a href="#">»</a></li>
	li=''
	li += '<li><a href="?p=1">首页</a></li>'
	if p>1:
		li += '<li><a href="?p='+str(p-1)+'">«</a></li>'
	for i in range(begin,end+1):
		if i==p:
			li+='<li class="am-active"><a href="?p='+str(i)+'">'+str(i)+'</a></li>'
		else:
			li+='<li><a href="?p='+str(i)+'">'+str(i)+'</a></li>'
	if p<end:
		li += '<li><a href="?p='+str(p+1)+'">»</a></li>'
	li += '<li><a href="?p='+str(count)+'">尾页</a></li>'

	return format_html(li)

# 会员分页
@register.simple_tag
def showPage(count,request):
	try:
		p = int(request.GET.get('p',1))
	except:
		p = 1
	begin = p-4
	end = p+5
	count = int(count) 
	if p<5:
		begin = 1
		end = 10
	if count<10:
		begin = 1
		end = count
	if p>count-5:
		begin = count-9
		end = count
	if begin<1:
		begin = 1
	# {% for i in data.paginator.page_range %}
 #                <li>
 #                  <a href="?p={{i}}">{{i}}</a></li>
 #                  {% endfor %}
                  # <li>

                  #   

                  # <li><a href="#">»</a></li>
	li=''
	add = request.GET
	news = ''
	for v,k in add.items():
		if v!='p':
			news+='&'+v+"="+k
	li += '<li><a href="?p=1'+news+'">首页</a></li>'
	if p>1:
		li += '<li><a href="?p='+str(p-1)+news+'">«</a></li>'
	for i in range(begin,end+1):
		if i==p:
			li+='<li class="am-active"><a href="?p='+str(i)+news+'">'+str(i)+'</a></li>'
		else:
			li+='<li><a href="?p='+str(i)+news+'">'+str(i)+'</a></li>'
	if p<end:
		li += '<li><a href="?p='+str(p+1)+news+'">»</a></li>'
	li += '<li><a href="?p='+str(count)+news+'">尾页</a></li>'

	return format_html(li)

# 前端商品   ---分页
@register.simple_tag
def showHomeGoods(count,request):
	try:
		p = int(request.GET.get('p',1))
	# 	sou = request.GET.get('sou')
	# 	if sou == None:
	# 		fen = str(request.GET.get('fen',1))
	# 		newf = "&fen="+fen
	# 		if request.GET.get('types') != None:
	# 			newf+="&types="+request.GET.get('types')
	# 	else:
	# 		fen = sou
	# 		newf = "&sou="+fen
	# 		if request.GET.get('types') != None:
	# 			newf+="&types="+request.GET.get('types')
	except:
		p=1
	add = request.GET
	newf = ''
	print(add)
	for v,k in add.items():
		if v!='p':
			newf+='&'+v+"="+k
	begin = p-4
	end = p+5
	if p<5:
		begin = 1
		end = 10
	if count<10:
		begin = 1
		end = count
	if p>count-5:
		begin = count-9
		end = count
	if begin<1:
		begin = 1
	# {% for i in data.paginator.page_range %}
 #                <li>
 #                  <a href="?p={{i}}">{{i}}</a></li>
 #                  {% endfor %}
                  # <li>

                  #   

                  # <li><a href="#">»</a></li>
	li=''
	li += '<li><a href="?p=1&fen='+newf+'">首页</a></li>'
	if p>1:
		li += '<li><a href="?p='+str(p-1)+newf+'">«</a></li>'
	for i in range(begin,end+1):
		if i==p:
			li+='<li><a href="?p='+str(i)+newf+'">'+str(i)+'</a></li>'
		else:
			li+='<li><a href="?p='+str(i)+newf+'">'+str(i)+'</a></li>'
	if p<end:
		li += '<li><a href="?p='+str(p+1)+newf+'">»</a></li>'
	li += '<li><a href="?p='+str(count)+newf+'">尾页</a></li>'

	return format_html(li)


# 购物车加减
@register.simple_tag
def jiajian(num1,num2):
	sum = num1+num2
	return '%.2f'%(sum)

@register.simple_tag
def xiaojie(num1,num2):
	arg = num1*num2
	return "%.2f"%(arg)